package com.github.pig.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.model.dto.ChannelSourceTree;
import com.github.pig.admin.model.dto.DeptTree;
import com.github.pig.admin.model.entity.ChannelSource;
import com.github.pig.admin.model.entity.SysDept;
import com.github.pig.admin.service.ChannelSourceService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/channelSource")
public class ChannelSourceController extends BaseController {

    @Autowired
    private ChannelSourceService service;

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public JSONArray getTree() {
        ChannelSource condition = new ChannelSource();
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        List<ChannelSourceTree> trees = service.selectListTree(new EntityWrapper<>(condition));
        String treeStr = JSONArray.toJSONString(trees);
        treeStr = treeStr.replaceAll("\"children\":\\[\\],", "");
        return JSONArray.parseArray(treeStr);
    }

    /**
     * 获取用户组织路径
     * @param id 渠道ID
     * @return 用户组织路径列表
     */
    @GetMapping(value = "/{id}")
    public List<Integer> getUserDeptPath(@PathVariable Integer id) {
        return service.getPathById(id);
    }


    /**
     * 获取用户组织路径
     * @param id 渠道ID
     * @return 用户组织路径列表
     */
    @GetMapping(value = "/getObj/{id}")
    public ChannelSource getObj(@PathVariable Integer id) {
        return service.selectById(id);
    }

    /**
     * 新增
     * @param channelSource
     * @return
     */
    @PostMapping
    public Boolean addObj(@RequestBody ChannelSource channelSource){
        channelSource.setCreateTime(new Date());
        channelSource.setUpdateTime(new Date());
        channelSource.setDelFlag(CommonConstant.STATUS_NORMAL);
        return service.insert(channelSource);
    }


    /**
     * 修改
     * @param channelSource
     * @return
     */
    @PutMapping
    public Boolean edit(@RequestBody ChannelSource channelSource){
        channelSource.setUpdateTime(new Date());
        return service.updateById(channelSource);
    }


    /**
     * 删除
     * @param channelSource
     * @return
     */
    @DeleteMapping
    public Boolean remove(@RequestBody ChannelSource channelSource){
        channelSource.setDelFlag(CommonConstant.STATUS_DEL);
        return service.updateById(channelSource);
    }
}
